//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;
using SR = Microsoft.Research.DataLayer;
using System.Windows.Media;
using System.Windows;
using System.Windows.Input;

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    /// <summary>
    /// Interaction logic for NodeEditorLeftPaneView.xaml
    /// </summary>
    public partial class NodeEditorLeftPaneView : UserControl
    {
        public NodeEditorLeftPaneView()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// The selected item changed event on the treeview.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The event arguments.</param>
        private void OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            (this.DataContext as NodeLeftPanePresenter).NodeSelectedCommand.Execute(e.NewValue as NodeViewModel);
        }

        /// <summary>
        /// Event handler for the PreviewMouseDown event on the category treeview. This event is needed
        /// because the selection changed event does not fire when alternating between 2 controls.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The event arguments.</param>
        private void OnGridPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            NodeLeftPanePresenter presenter = this.DataContext as NodeLeftPanePresenter;
            if (!presenter.IsSelected && treeview.SelectedItem != null)
            {
                Grid selectedGrid = sender as Grid;
                if (selectedGrid != null)
                {
                    NodeViewModel model = selectedGrid.DataContext as NodeViewModel;

                    // If the mousedown happens on the currently selected item then fire the selected command.
                    if (treeview.SelectedItem.Equals(model))
                    {
                        presenter.NodeSelectedCommand.Execute(model);
                    }
                }
            }
        }
    }

    /// <summary>
    /// The converter for the image to be shown in the node list for the node.
    /// </summary>
    public class ExecuterStatusImageSourceConverter1 : IMultiValueConverter
    {
        /// <summary>
        /// The path of the image to be shown if the status is online.
        /// </summary>
        private const string OnlineImageSource =
            @"pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/ManagementStudio/nodeOnline.png";

        /// <summary>
        /// The path of the image to be shown if the status is offline.
        /// </summary>
        private const string OfflineImageSource =
            @"pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/ManagementStudio/nodeOffline.png";

        /// <summary>
        /// The path of the image to be shown if the status is faulty.
        /// </summary>
        private const string FaultyImageSource =
            @"pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/ManagementStudio/nodeError.png";

        /// <summary>
        /// The node image with no status depiction.
        /// </summary>
        private const string StaticNodeImage =
            @"pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/ManagementStudio/nodes.png";

        /// <summary>
        /// A node which is not an executor.
        /// </summary>
        private const string NonExecutorImage =
            @"pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/ManagementStudio/nodeCompute.png";
        
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = OnlineImageSource;
            SR.MachineStatus machineStatus = (SR.MachineStatus)values[0];
            SR.ExecutorAvailabiltiy availability = (SR.ExecutorAvailabiltiy)values[1];
            NodeViewModel nodeModel = values[2] as NodeViewModel;
            if (nodeModel != null && !nodeModel.CompleteNodeViewModel)
            {
                return (new ImageSourceConverter()).ConvertFromString(StaticNodeImage);
            }

            if (nodeModel.MachineType == SR.MachineType.HPCComputeNode)
            {
                result = NonExecutorImage;
            }
            else
            {
                if (availability == SR.ExecutorAvailabiltiy.Offline)
                {
                    result = OfflineImageSource;
                }
                else
                {
                    result = (machineStatus == SR.MachineStatus.Faulty) ? FaultyImageSource : OnlineImageSource;
                }
            }

            return (new ImageSourceConverter()).ConvertFromString(result);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
